const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');
const crypto = require('../utils/crypto');

const Message = sequelize.define('Message', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  content: {
    type: DataTypes.TEXT,
    allowNull: false,
    set(value) {
      // 消息内容加密存储
      this.setDataValue('content', crypto.encrypt(value));
    },
    get() {
      // 获取时解密
      const encrypted = this.getDataValue('content');
      return encrypted ? crypto.decrypt(encrypted) : null;
    }
  },
  roomId: {
    type: DataTypes.STRING(50),
    allowNull: false
  },
  filePath: {
    type: DataTypes.STRING(255)
  },
  fileType: {
    type: DataTypes.ENUM('image', 'video', 'document')
  }
}, {
  indexes: [
    {
      fields: ['roomId']
    },
    {
      fields: ['createdAt']
    }
  ],
  timestamps: true
});

module.exports = Message;